
export const projectoptions = ref([])      // 项目列表
export const homeData = ref('')            // 总数据

export const projectID = ref('')  // 当前项目的id
export const projectName = computed(() => {
  let item = projectoptions.value.find((v) => v.key == projectID.value)
  return item?.label ?? ''
})
export const isShowNewList = ref(true)

// 左上数据
export const leftabData = ref([]) // tabs
export const radarIndicator = ref([])
export const radarValue = ref([])
export const leftBottomData = ref([])
export const leftBottomRHZData = ref([])
export const p2 = ref('')
export const p6 = ref('')


export const leftYouPingMessage = ref('')

export const switchloading1 = ref(false)
export const switchloading2 = ref(false)
export const switchvalue1 = ref(false)
export const switchvalue2 = ref(false)


// 中间数据
export const config = ref({
  header: ['<span style="color:#FFFFFF;">监测类型</span>', '<span style="color:#FFFFFF;">数值</span>', '<span style="color:#FFFFFF;">状态</span>'],
  data: [],
  rowNum: 10,
  headerBGC: '#009757',
  oddRowBGC: '#a0e1ac',
  evenRowBGC: '#f3fdf1',
  headerHeight: 45,
  columnWidth: [140],
  // waitTime: 500000,
  align: [
    'left',
    'center',
    'center',
  ]
})


// 报警数据
export const errList = ref([])
export const configErr = ref({
  header: ['<span style="color:#FFFFFF;">项目名称</span>', '<span style="color:#FFFFFF;">时间</span>', '<span style="color:#FFFFFF;">告警</span>'],
  data: [],
  rowNum: 3,
  headerBGC: '#009757',
  oddRowBGC: '#a0e1ac',
  evenRowBGC: '#f3fdf1',
  headerHeight: 45,
  columnWidth: [],
  // waitTime: 500000,
  align: [
    'left',
    'center',
    'center',
  ]
})
// 获取告警
export const getErrorData = () => {
  errList.value = []
  TAlarmIntimeControllerSelTAlaimIntime().then(res => {
    if (!res) return
    errList.value = res.result
    // 中间数据处理
    let centerConfig = []
    centerConfig = res.result.map(v => {
      return [`<span style="color:#000000;">${v.projectName}</span>`, `<span style="color:#000000;">${v.updateTime
        }</span>`, `<span style="color:${v.status == '正常' ? '#000000' : '#000000'};">${v.reason}</span>`]
    })
    if (JSON.stringify(config.value.data) !== JSON.stringify(centerConfig)) {
      configErr.value.data = centerConfig
    }

  })
}





export const config1 = ref({})


// 图表
export const lineData = ref([])
export const Apage = ref([])
export const Bpage = ref([])



export const getprojectData = () => {
  tProjectControllerSellist().then(res => {
    projectoptions.value = res.result.map(v => {
      return {
        key: v.id,
        label: v.name,
      }
    })
  })
}


export const loading = ref(true)


export const init = () => {
  console.log('init')
  // 获取项目列表
  tProjectControllerSellist().then(res => {
    projectoptions.value = res.result.map(v => {
      return {
        key: v.id,
        label: v.name,
      }
    })
    if (projectID.value) {

    } else {
      projectID.value = projectoptions.value[0].key
      projectName.value = projectoptions.value[0].label
    }


    gethomeData(true)

  })
}

export const firstload = ref(true)
export const refreshLoading = ref(true)

export const qttype = ref(false)
export const btype = ref(false)
export const szkgtype = ref(false)
export const bid = ref('')
export const yl = ref('-')

// 新加电池类型
export const batteryType = ref(0) //	油品寿命区域数据类型 0 实时告警数据 1 电池油品寿命 2 玄武双电池
export const batteryDdlpyl = ref({})//玄武电导率偏移率
export const batteryJdcspyl = ref({})//玄武介电常数偏移率

export const realTimeData = ref([])//中间实时数据
// 获取详细数据
export const gethomeData = (isLoading) => {
  refreshLoading.value = true
  if (isLoading) {
    loading.value = true
  } else {
    loading.value = firstload.value
  }
  const userStore = useUserStore()
  // projectID.value = '8b60cb7bf3314a429138833a76cc7076'
  // loading.value = true
  HomePageControllerPicture1(projectID.value, userStore.timeValue).then(res => {
    console.log(res, 'res')
    homeData.value = res.result

    leftabData.value = res.result.p1.data.tablist
    radarIndicator.value = res.result.p1.radar.indicator
    radarValue.value = res.result.p1.radar.value
    // radarIndicator.value = []
    // radarValue.value = []
    leftYouPingMessage.value = res.result.p2


    p2.value = res.result.p2
    leftBottomData.value = [
      { name: '油品名称 :', value: res.result.p2.oilName || '', type: 2 },
      // { name: '滤油器状态 :', value: res.result.p2.oilStatus || 0, type: 2 },
      { name: '油品容量 :', value: res.result.p2.oilLiter || '', type: 2 },
      { name: '油品牌号 :', value: res.result.p2.oilBrand || '', type: 2 },
      { name: '加注时间 : ', value: res.result.p2.addOilTime || '', type: 0 },
      { name: '建议换油时间 :', value: res.result.p2.updateOilTime || '', type: 1 },
    ]

    leftBottomRHZData.value = [
      { name: '润滑脂名称 :', value: res.result.p6?.oilName || '', show: true, showLabel: true },
      { name: '润滑脂容量 :', value: res.result.p6?.oilLiter || '', show: true, showLabel: true },
      { name: '润滑脂牌号 :', value: res.result.p6?.oilBrand || '', show: true, showLabel: true },
      { name: '运行时长 : ', value: res.result.p6?.yxtime || '', show: true, showLabel: true },
      { name: '间隔时长 :', value: res.result.p6?.tztime || '', show: true, showLabel: true },
      { name: '功率 :', value: res.result.p6?.gl || '', show: true, showLabel: true },
      { name: '泵状态 :', value: res.result.p6?.islink || '', show: false, showLabel: false },
      { name: '启停 :', value: res.result.p6?.qtzt || '', showLabel: false },
      { name: '自动/手动 :', value: res.result.p6?.zbzt || '', showLabel: false },
    ]
    p6.value = res.result.p6


    // 新加润滑脂
    bid.value = res.result.p6?.bid
    qttype.value = res.result.p6?.qtzt == 1 ? true : false
    btype.value = res.result.p6?.islink == 1 ? true : false
    szkgtype.value = res.result.p6?.zbzt == 1 ? true : false

    yl.value = res.result.p6?.yl


    // 滤油机状态
    switchvalue1.value = res.result.p2.oilControll == 1 ? true : false
    // 油泵控制
    switchvalue2.value = res.result.p2.bengControll == 1 ? true : false



    // 中间数据处理
    realTimeData.value = res.result.p3


    let centerConfig = []
    centerConfig = res.result.p3.map(v => {
      return [`<span style="color:#000000;">${v.type}</span>`, `<span style="color:#000000;">${v.number
        }</span>`, `<span style="color:${v.status == '正常' ? '#000000' : '#000000'};">${v.status}</span>`]
    })
    if (JSON.stringify(config.value.data) !== JSON.stringify(centerConfig)) {
      config.value.data = centerConfig
    }

    if (res.result.p4 >= 60) {
      config1.value = {
        value: res.result.p4,
        // colors: ['#a0e1ac', '#064e3b'],
        colors: ['#00ff00', '#064e3b'],
        textColor: ['#1f2937']
      }
    } else if (res.result.p4 <= 30) {
      config1.value = {
        value: res.result.p4,
        // colors: ['#ef4444', '#831843'],
        colors: ['#f87171', '#991b1b'],
        textColor: ['#1f2937']
      }
    } else {
      config1.value = {
        value: res.result.p4,
        // colors: ['#fde68a', '#d97706'],
        colors: ['#fcd34d', '#d6964c'],
        textColor: ['#1f2937']
      }
    }



    batteryType.value = res.result.qylx
    batteryDdlpyl.value = res.result.ddlpyl
    batteryJdcspyl.value = res.result.jdcspyl
    if (batteryType.value == 0) {
      batteryDdlpyl.value = {}
      batteryJdcspyl.value = {}
    } else if (batteryType.value == 1) {
      batteryDdlpyl.value = {}
      batteryJdcspyl.value = {}
    } else {


      if (res.result.ddlpyl < 10) {
        batteryDdlpyl.value = {
          value: res.result.ddlpyl,
          colors: ['#00ff00', '#064e3b'],
          textColor: ['#1f2937']
        }
      } else if (res.result.ddlpyl > 30) {
        batteryDdlpyl.value = {
          value: res.result.ddlpyl,
          colors: ['#f87171', '#991b1b'],
          textColor: ['#1f2937']
        }
      } else {
        batteryDdlpyl.value = {
          value: res.result.ddlpyl,
          colors: ['#fcd34d', '#d6964c'],
          textColor: ['#1f2937']
        }
      }




      if (res.result.jdcspyl < 10) {
        batteryJdcspyl.value = {
          value: res.result.jdcspyl,
          colors: ['#00ff00', '#064e3b'],
          textColor: ['#1f2937']
        }
      } else if (res.result.jdcspyl > 30) {
        batteryJdcspyl.value = {
          value: res.result.jdcspyl,
          colors: ['#f87171', '#991b1b'],
          textColor: ['#1f2937']
        }
      } else {
        batteryJdcspyl.value = {
          value: res.result.jdcspyl,
          colors: ['#fcd34d', '#d6964c'],
          textColor: ['#1f2937']
        }
      }






    }


    // 折线图处理
    lineData.value = res.result.p5
    let A = []
    let B = []
    res.result.p5.forEach((v, i) => {
      if (i < 3) {
        A.push(v)
      } else {
        B.push(v)
      }
    });
    Apage.value = A
    Bpage.value = B





    firstload.value = false
    console.log('2222')
  }).finally(() => {
    loading.value = false
    refreshLoading.value = false
    getErrorData()
  })
}

